package com.fileshare.web.controller.admin.api;

import com.fileshare.web.dto.Bo.AuthorityBo;
import com.fileshare.web.dto.Bo.RoleBo;
import com.fileshare.web.dto.Result;
import com.fileshare.web.dto.Vo.AuthorityVo;
import com.fileshare.web.dto.Vo.RoleVo;
import com.fileshare.web.entity.Authority;
import com.fileshare.web.entity.Role;
import com.fileshare.web.service.AuthService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限
 */
@RestController
@RequestMapping("/admin/api/auth")
@ResponseBody
@PreAuthorize("@auth.matchAny('administrator')")
public class AuthenticationApiController {

    @Resource
    private AuthService authService;

    @PostMapping("/role")
    public Result<RoleVo> addRole(@RequestBody RoleBo roleBo) {
        return Result.success().with(authService.addRole(roleBo));
    }

    @DeleteMapping("/role/{id}")
    public Result<RoleVo> deleteRole(@PathVariable Integer id) {
        return Result.success().with(authService.removeRole(id));
    }

    @PutMapping("/role")
    public Result<RoleVo> updateRole(@RequestBody RoleBo roleBo) {
        return Result.success().with(authService.updateRole(roleBo));
    }

    @GetMapping("/user/{id}/roles")
    public Result<List<RoleVo>> userRoles(@PathVariable Integer id) {
        return Result.success().with(authService.getUserRoles(id));
    }

    @GetMapping("/user/roles")
    public Result<List<Role>> roles() {
        return Result.success().with(authService.getAllRoles());
    }

    @PostMapping("/user/{id}/roles")
    public Result<List<RoleVo>> saveUserRoles(@PathVariable Integer id,@RequestBody List<String> roles) {
        return Result.success().with(authService.saveUserRoles(id,roles));
    }

    @GetMapping("/role/{id}/authorities")
    public Result<List<Authority>> roleAuthorities(@PathVariable Integer id) {
        return Result.success().with(authService.getAllRoleAuthorities(id));
    }

    @PostMapping("/role/{id}/authorities")
    public Result<List<Authority>> saveRoleAuthorities(@PathVariable Integer id,@RequestBody List<String> roleAuthorities) {
        return Result.success().with(authService.saveRoleAuthorities(id,roleAuthorities));
    }

    @PostMapping("/authority")
    public Result<AuthorityVo> addAuthority(@RequestBody AuthorityBo authorityBo) {
        return Result.success().with(authService.addAuthority(authorityBo));
    }

    @DeleteMapping("/authority/{id}")
    public Result<AuthorityVo> deleteAuthority(@PathVariable Integer id) {
        return Result.success().with(authService.removeAuthority(id));
    }

    @PutMapping("/authority")
    public Result<AuthorityVo> updateAuthority(@RequestBody AuthorityBo authorityBo) {
        return Result.success().with(authService.updateAuthority(authorityBo));
    }
}
